package com.zfj.Collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author zfj
 * @date 2022/3/4 -15:39
 */
public class Demo2 {
	public static void main(String[] args) {
		//新建Collection对象
		Collection collection=new ArrayList();
		//添加学生对象
		Student s1=new Student("张三",20);
		Student s2=new Student("李四",22);
		Student s3=new Student("王五",25);
		collection.add(s1);
		collection.add(s2);
		collection.add(s3);
		System.out.println(collection.size());
		System.out.println(collection.toString());
		//删除
		collection.remove(s1);
		System.out.println("删除之后:"+collection.toString());
		//遍历
		//增强for
		for (Object o : collection) {
			Student s=(Student)o;
			System.out.println(s.toString());
		}
		//迭代器
		//hasNext()有没有下一个元素
		//next()获取下一个元素
		//remove()删除当前元素
		System.out.println("========迭代器========");
		Iterator it=collection.iterator();
		while (it.hasNext()){
			Student object=(Student)it.next();
			System.out.println(object);
			//ConcurrentModificationException并发修改异常
//			collection.remove(object);
//			it.remove();
		}
		// 判断
		System.out.println(collection.contains(s1));
		System.out.println(collection.isEmpty());
	}
}
